<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑页面</title>
</head>
<body>
<script type="text/html" id="form">
    <input type="hidden" name="id" value="{{= d.id }}" />
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
				<div class="layui-form-item">
    				<label class="layui-form-label" title="手机号"> <text title="必填项" style="color:red;">*</text> 手机号</label>
    				<div class="layui-input-block">
            			<input autocomplete="off" type="text" name="phone" id="phone" placeholder="请输入手机号" value="{{=d.phone}}" class="layui-input" lay-verify="required|phone">
    				</div>
				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label" title="姓名"> <text title="必填项" style="color:red;">*</text> 姓名</label>
    				<div class="layui-input-block">
            			<input autocomplete="off" type="text" name="name" id="name" placeholder="请输入姓名" value="{{=d.name}}" class="layui-input" lay-verify="required">
    				</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">密码</label>
					<div class="layui-input-block">
						<input type="password" name="password" autocomplete="off" placeholder="请输入密码" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">重复密码</label>
					<div class="layui-input-block">
						<input type="password" name="re_password" autocomplete="off" placeholder="请输入重复密码" class="layui-input">
					</div>
				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label" title="性别"> <text title="必填项" style="color:red;">*</text> 性别</label>
    				<div class="layui-input-block">
            			<input type="radio" name="sex" id="sex-1" value="1" title="男" {{#if (d.sex == '1'){ }}checked="checked"{{# } }} />
						<input type="radio" name="sex" id="sex-2" value="2" title="女" {{#if (d.sex == '0'){ }}checked="checked"{{# } }} />
    				</div>
				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label" title="身份"> <text title="必填项" style="color:red;">*</text>身份</label>
    				<div class="layui-input-block">
            			<select name="type" id="type" lay-verify="required" placeholder="身份">
							<option value="">请选择身份</option>
							<option value="1" {{# if(d.type.toString() == '1'){ }}selected="selected"{{# } }}>Boss</option>
							<option value="2" {{# if(d.type.toString() == '2'){ }}selected="selected"{{# } }}>Leader</option>
							<option value="3" {{# if(d.type.toString() == '3'){ }}selected="selected"{{# } }}>Phper</option>
							<option value="4" {{# if(d.type.toString() == '4'){ }}selected="selected"{{# } }}>Gofer</option>
                        </select>
    				</div>
				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label" title="身份证号码"> <text title="必填项" style="color:red;">*</text> 身份证号码</label>
    				<div class="layui-input-block">
            			<input autocomplete="off" type="text" name="id_card" id="id_card" placeholder="请输入身份证号码" value="{{=d.id_card}}" class="layui-input" lay-verify="required|identity">
    				</div>
				</div>

				<!-- 新增组织下拉选项 -->
				<div class="layui-form-item">
					<label class="layui-form-label">组织</label>
					<div class="layui-input-block">
						<select name="org_id" lay-filter="organization">
							<option value="">请选择组织</option>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
    				<label class="layui-form-label" title="状态"> <text title="必填项" style="color:red;">*</text> 状态</label>
    				<div class="layui-input-block">
            			<!-- 隐藏域设置开关未选中时需要传递的参数值 -->
                        <input type="hidden" name="status" id="status-0" value="0" />
                        <input type="checkbox" name="status" id="status-1" lay-skin="switch" lay-text="正常|封禁" value="1" {{# if(1 == d.status){ }}checked="checked"{{# } }} />
    				</div>
				</div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="laytp-btn laytp-btn-primary laytp-btn-sm" lay-submit="" lay-filter="laytp-form">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="laytp-btn laytp-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</script>
<form class="layui-form" lay-filter="layui-form"></form>
<script>
    if(localStorage.getItem("staticDomain")){
        document.write("<link rel='stylesheet' href='" + localStorage.getItem("staticDomain") + "/component/laytp/css/laytp.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/layui/layui.js?v=" + Date.now() + "'><\/script>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/laytp/layuiConfig.js?v=" + Date.now() + "'><\/script>");
    }else{
        document.write("<link rel='stylesheet' href='/static/component/laytp/css/laytp.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='/static/component/layui/layui.js?v=" + Date.now() + "'><\/script>");
        document.write("<script src='/static/component/laytp/layuiConfig.js?v=" + Date.now() + "'><\/script>");
    }
</script>
<script>
    layui.use(['laytp'],function(){
        let form = layui.form;
        let $ = layui.jquery;

        //获取参数ID
        var id = facade.getUrlParam('id');
        if(!id){
            facade.error('参数ID不能为空');
            return false;
        }

        //获取数据，渲染到对应的节点上
        facade.ajax({
            route: "/admin.students/info",
            data: {id: id},
            successAlert: false,
            showLoading: true
        }).done(function(res){
            if(res.code === 0){
                layui.laytpl($("#form").html()).render(res.data,function(string){
                    $("form").html(string);
                    layui.laytpForm.render();
                    form.render();
					// 动态加载组织数据
					facade.ajax({
						route: '/admin.org/index',
						successAlert: false
					}).done(function (orgRes) {
						if (orgRes.code === 0) {
							let select = $('select[name="org_id"]');
							orgRes.data.forEach(function (item) {
								// 设置选中项
								let selected = (item.id == res.data.org_id) ? 'selected' : ''; // 注意使用 == 而不是 ===
								select.append(`<option value="${item.id}" ${selected}>${item.name}</option>`);
							});
							form.render('select'); // 刷新下拉框样式

						}
					});
                });
            }
        });

        form.on('submit(laytp-form)', function(data){
            var btnAnim = layui.button.load({elem:$(this)});
            try{
                data = facade.setEditorField(data);
            }catch (e) {
                facade.error(e);
                return false;
            }
            facade.ajax({
                route:'/admin.students/edit',
                data:data.field
            }).done(function(res){
                if(res.code === 0){
                    parent.layui.layer.close(parent.layui.layer.getFrameIndex(window.name));//关闭当前页
                    parent.layui.table.reloadData("laytp-table");
                }
                btnAnim.stop();
            }).fail(function(){
                btnAnim.stop();
            });
            return false;
        });
    })
</script>
</body>
</html>